package jsyf.oa.web.wechat.service_account.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.wechat.service_account.WechatTemplateFacade;
import jsyf.oa.facade.wechat.service_account.WechatUserFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/template")
public class TemplatePrivateController {

    @Resource
    private WechatUserFacade wechatUserFacade;

    @Resource
    private WechatTemplateFacade wechatTemplateFacade;

    /**
     * 服务号-获取已添加模板消息列表
     */
    @RequestMapping(value = "/getPrivateTemplate.json", method = RequestMethod.POST)
    public void getPrivateTemplate(HttpServletResponse response, @RequestBody String jsonData) {

        JSONObject jsonObject = wechatTemplateFacade.getPrivateTemplate();
        if (jsonObject != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjTimeRespond(jsonObject));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("未找到数据"));
        }
    }

    /**
     * 服务号-发送模板消息
     * @param response
     * @param jsonData
     */
    @RequestMapping(value = "/sendPrivateTemplate.json", method = RequestMethod.POST)
    public void sendPrivateTemplate(HttpServletResponse response, @RequestBody String jsonData) throws IOException {

        //  调用接口-获取open_id
        List<JSONObject> users = wechatUserFacade.getAllUser();
        ArrayList<String> openIds = new ArrayList<>();
        for (int i = 0; i < users.size(); i++) {
            JSONObject user = users.get(i);
            openIds.add((String) user.get("openId"));
        }
        //  发送模板消息
        int errorCount = 0;
        int successCount = 0;
        Map map = (Map) JSON.parse(jsonData);
        // 遍历open_id，发送模板消息
        for (String open_id : openIds) {
            //  在arr中拼接open_id
            map.put("touser", open_id);
            jsonData = map.toString();
            JSONObject jsonObject = wechatTemplateFacade.sendPrivateTemplate(jsonData);
            if (jsonObject != null) {
                if (0 != jsonObject.getInteger("errcode")) {
                    errorCount++;
                } else {
                    successCount++;
                }
            }
        }
        //  接口返回数据
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errorCount",errorCount+"");
        jsonObject.put("successCount",successCount+"");
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonObject));
    }
}